//
//  RegisterAccountViewController.m
//  EZ Tracker
//
//  Created by FENGYL on 2016/11/1.
//  Copyright © 2016年 FENGYL. All rights reserved.
//

#import "RegisterAccountViewController.h"
#import "MsgCommand.h"

#import <SVProgressHUD.h>

#define NUM @"0123456789"

@interface RegisterAccountViewController ()<UITextFieldDelegate>

@property (strong, nonatomic) MsgCommand *msgCommand;

@property (weak, nonatomic) IBOutlet UIButton *registerButton;

@property (weak, nonatomic) IBOutlet UITextField *deviceCountryCodeTextfield;
@property (weak, nonatomic) IBOutlet UITextField *devicePhoneNumberTextfield;
@property (weak, nonatomic) IBOutlet UITextField *masterCountryCodeTextfield;
@property (weak, nonatomic) IBOutlet UITextField *masterPhoneNumberTextfield;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextfield;
@property (weak, nonatomic) IBOutlet UITextField *confirmPasswordTextfield;

@end

@implementation RegisterAccountViewController

-(MsgCommand *)msgCommand{
    
    if (!_msgCommand) {
        _msgCommand = [[MsgCommand alloc]init];
    }
    return _msgCommand;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
    
}

#pragma mark 设置UI
-(void)setUpUI{
    
    //设置navigation
    [self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
    
    //圆角button
    self.registerButton.layer.masksToBounds = YES;
    
    self.registerButton.layer.cornerRadius = 10;
    
    //设置代理
    self.deviceCountryCodeTextfield.delegate = self;
    self.devicePhoneNumberTextfield.delegate = self;
    self.masterCountryCodeTextfield.delegate = self;
    self.masterPhoneNumberTextfield.delegate = self;
    
}

-(void)viewWillDisappear:(BOOL)animated{
    
    //pop时变回蓝色
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0/255.0 green:80/255.0 blue:255/255.0 alpha:1]];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
    
}

#pragma mark 点击收起键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.deviceCountryCodeTextfield resignFirstResponder];
    [self.devicePhoneNumberTextfield resignFirstResponder];
    [self.masterCountryCodeTextfield resignFirstResponder];
    [self.masterPhoneNumberTextfield resignFirstResponder];
    [self.passwordTextfield resignFirstResponder];
    [self.confirmPasswordTextfield resignFirstResponder];
    
    
}

#pragma mark 限制类型
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.deviceCountryCodeTextfield || textField == self.masterCountryCodeTextfield || textField == self.devicePhoneNumberTextfield || textField == self.masterPhoneNumberTextfield) {
        
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUM] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
        
    }
    
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark registerButton
- (IBAction)touchRegister:(id)sender {
    
    if (!(_devicePhoneNumberTextfield.text.length > 0)) {
        
        [SVProgressHUD showErrorWithStatus:@"Device Phone Number can not be empty"];
        
        [SVProgressHUD dismissWithDelay:1];
        
        return;
    }
    
    if (!(_masterPhoneNumberTextfield.text.length > 0)) {
        
        [SVProgressHUD showErrorWithStatus:@"Master Phone Number can not be empty"];
        
        [SVProgressHUD dismissWithDelay:1];
        
        return;
        
    }
    
    if (!(_passwordTextfield.text.length > 0 )) {
        
        [SVProgressHUD showErrorWithStatus:@"Password can not be empty"];
        
        [SVProgressHUD dismissWithDelay:1];
        
        return;
        
    }
    
    if (!(_confirmPasswordTextfield.text.length > 0)) {
        
        [SVProgressHUD showErrorWithStatus:@"Confirm Password can not be empty"];
        
        [SVProgressHUD dismissWithDelay:1];
        
        return;
        
    }
    
    if (![_confirmPasswordTextfield.text isEqualToString:_passwordTextfield.text]) {
        
        [SVProgressHUD showErrorWithStatus:@"Two password are not match.Please try."];
        
        [SVProgressHUD dismissWithDelay:1];
        
        return;
        
    }
    
    //号码
    NSString *finalPhoneNumber;
    
    NSString *countryCodeString = _deviceCountryCodeTextfield.text;
    NSString *phoneNumberString = _devicePhoneNumberTextfield.text;
    
    if (countryCodeString.length > 0) {
        
        finalPhoneNumber = [NSString stringWithFormat:@"+%@%@",countryCodeString,phoneNumberString];
        
    }else{
        
        finalPhoneNumber = [NSString stringWithFormat:@"%@%@",countryCodeString,phoneNumberString];
        
    }
    
    
    //master number
    NSString *masterPhoneNumber;
    
    NSString *masterCountryCodeString = _masterCountryCodeTextfield.text;
    NSString *masterPhoneNumberString = _masterPhoneNumberTextfield.text;
    
    if (masterCountryCodeString.length > 0) {
        
        masterPhoneNumber = [NSString stringWithFormat:@"+%@%@",masterCountryCodeString,masterPhoneNumberString];
        
    }else{
        
        masterPhoneNumber = [NSString stringWithFormat:@"%@%@",masterCountryCodeString,masterPhoneNumberString];
        
    }
    
    //body
    NSMutableString *bodyString = [NSMutableString string];
    
    [bodyString appendString:@"*master*"];
    
    [bodyString appendString:_confirmPasswordTextfield.text];
    
    [bodyString appendString:@"*"];
    
    [bodyString appendString:masterPhoneNumber];
    
    [bodyString appendString:@"*"];

    
    [self.msgCommand showMessageViewForPhoneNumberArray:@[phoneNumberString] title:@"New Message" body:bodyString delegate:self];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
